/**
 * 
 */

window.onload = function() {
	document.getElementById("price").onclick = function() {
		setPara("order", "price");
	};
	document.getElementById("volume").onclick = function() {
		setPara("order", "volumn");
	};
	document.getElementById("score").onclick = function() {
		setPara("order", "score");
	};
	
	document.getElementById("popular").onclick = function() {
		var checked = document.getElementById("popular").checked;
		if (checked) {
			setPara("filter", "popular");
		} else {
			delPara("filter", "popular");
		}
	};
	document.getElementById("promote").onclick = function() {
		var checked = document.getElementById("promote").checked;
		if (checked) {
			setPara("filter", "promote");
		} else {
			delPara("filter", "promote");
		}
	};
	document.getElementById("exist").onclick = function() {
		var checked = document.getElementById("exist").checked;
		if (checked) {
			setPara("filter", "exist");
		} else {
			delPara("filter", "exist");
		}
	};
};

function delPara(paraName, paraValue) {
	var href = location.href;
	var oldPara = href.substr(href.indexOf("&" + paraName + "=" + paraValue));
	var newPara = "";
	href = href.replace(oldPara, newPara);
	location.href = href;
}

function setPara(paraName, paraValue) {
	var href = location.href;
	if (href.indexOf("order") == -1 && href.indexOf("filter") == -1) {
		// only key
		href = href + "&" + paraName + "=" + paraValue;
	}
	else if (href.indexOf("order") != -1 && href.indexOf("filter") == -1) {
		// key & order
		if (paraName == "order") {
			var oldPara = href.substr(href.indexOf("order"));
			var newPara = "order=" + paraValue;
			href = href.replace(oldPara, newPara);
		}
		else if (paraName == "filter") {
			href = href + "&filter=" + paraValue;
		}
	}
	else if (href.indexOf("order") == -1 && href.indexOf("filter") != -1) {
		// key & filter
		if (paraName == "order") {
			var oldPara = href.substr(href.indexOf("filter"));
			var newPara = "order=" + paraValue;
			href = href.replace(oldPara, newPara + "&" + oldPara);
		}
		else if (paraName == "filter") {
			var oldPara = href.substr(href.indexOf("filter"));
			var newPara = "filter=" + paraValue;
			href = href.replace(oldPara, newPara);
		}
	}
	else {
		// key & order & filter
		if (paraName == "order") {
			var start = href.indexOf("order");
			var end = href.indexOf("filter") - 2;
			var oldPara = href.substr(start, end - start + 1);
			var newPara = "order=" + paraValue;
			href = href.replace(oldPara, newPara);
		}
		else if (paraName == "filter") {
			var oldPara = href.substr(href.indexOf("filter"));
			var newPara = "filter=" + paraValue;
			href = href.replace(oldPara, newPara);
		}
	}
	location.href = href;
}
